package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.common.util.CheckUtil;
import com.zoo.diannaopeijian.domain.ShipmentItem;
import com.zoo.diannaopeijian.mapper.ShipmentItemMapper;
import com.zoo.diannaopeijian.service.ShipmentItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 出货项服务
 */
@Service
public class ShipmentItemServiceImpl implements ShipmentItemService {
    @Resource
    private ShipmentItemMapper shipmentItemMapper;

    @Override
    public boolean insertShipmentItem(ShipmentItem shipmentItem) {
        if (CheckUtil.allNullBeanCheck(shipmentItem)) {
            return true;
        }
        return shipmentItemMapper.insertSelective(shipmentItem) == 1;
    }

    @Override
    public boolean insertShipmentItems(List<ShipmentItem> shipmentItemList) {
        if (shipmentItemList == null || shipmentItemList.size() == 0) {
            return true;
        }
        return shipmentItemMapper.insertList(shipmentItemList) > 0;
    }

    @Override
    public List<Map<String, Object>> getShipmentItemsByShiptRecordId(Integer shipmentRecordId) {
        if (shipmentRecordId == null) {
            throw new NullPointerException();
        }
        return shipmentItemMapper.selectShipmentItemsByShiptRecordId(shipmentRecordId);
    }

    @Override
    public List<ShipmentItem> deleteShipmentItems(ShipmentItem shipmentItem) {
        if (CheckUtil.allNullBeanCheck(shipmentItem)) {
            throw new RuntimeException("当前正欲执行清空数表的非法操作");
        }

        List<ShipmentItem> itemBeDeleted = shipmentItemMapper.select(shipmentItem);

        int numsBeDelete = shipmentItemMapper.delete(shipmentItem);

        if (itemBeDeleted.size() != numsBeDelete) {
            // 要删除的数据被其他事务先删除了，要不要做什么东西呢
        }

        return itemBeDeleted;
    }
}
